package com.liqi.web.controller.device;

import com.liqi.common.annotation.Log;
import com.liqi.common.core.controller.BaseController;
import com.liqi.common.core.domain.AjaxResult;
import com.liqi.common.core.page.TableDataInfo;
import com.liqi.common.enums.BusinessType;
import com.liqi.common.utils.DateUtils;
import com.liqi.common.utils.poi.ExcelUtil;
import com.liqi.common.utils.uuid.GenerateCodeUtils;
import com.liqi.deployment.domain.DvDeployment;
import com.liqi.deployment.domain.DvOperationUser;
import com.liqi.deployment.service.IDvDeploymentService;
import com.liqi.system.service.device.IDvOperationUserService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 运维人员信息Controller
 *
 * @author Lwy
 * @date 2022-02-16
 */
@Api(tags = "运维人员信息")
@RestController
@RequestMapping("/device/operationUser")
public class DvOperationUserController extends BaseController
{
    @Autowired
    private IDvOperationUserService dvOperationUserService;

    @Autowired
    private IDvDeploymentService dvDeployDeviceManagerService;

    /**
     * 查询运维人员信息列表
     */
    /*@PreAuthorize("@ss.hasPermi('device:operation:list')")*/
    @GetMapping("/listForDeploy")
    public TableDataInfo listForDeploy(DvOperationUser dvOperationUser)
    {
        /*startPage();*/
        List<DvOperationUser> list = dvOperationUserService.selectDvOperationUserListForDevice(dvOperationUser);
        return getDataTable(list);
    }

    /**
     * 查询运维人员信息列表
     */
    @PreAuthorize("@ss.hasPermi('device:operation:list')")
    @GetMapping("/list")
    public TableDataInfo list(DvOperationUser dvOperationUser)
    {
        startPage();
        List<DvOperationUser> list = dvOperationUserService.selectDvOperationUserList(dvOperationUser);
        return getDataTable(list);
    }

    /**
     * 查询运维人员信息列表
     */
    @PreAuthorize("@ss.hasPermi('device:operation:listNotPage')")
    @GetMapping("/listNotPage")
    public TableDataInfo listNotPage(DvOperationUser dvOperationUser)
    {
        List<DvOperationUser> list = dvOperationUserService.selectDvOperationUserList(dvOperationUser);
        return getDataTable(list);
    }

    /**
     * 导出运维人员信息列表
     */
    @PreAuthorize("@ss.hasPermi('device:operation:export')")
    @Log(title = "运维人员信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DvOperationUser dvOperationUser)
    {
        List<DvOperationUser> list = dvOperationUserService.selectDvOperationUserList(dvOperationUser);
        ExcelUtil<DvOperationUser> util = new ExcelUtil<DvOperationUser>(DvOperationUser.class);
        util.exportExcel(response, list, "运维人员信息数据");
    }

    /**
     * 获取运维人员信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('device:operation:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(dvOperationUserService.selectDvOperationUserById(id));
    }

    /**
     * 新增运维人员信息
     */
    @PreAuthorize("@ss.hasPermi('device:operation:add')")
    @Log(title = "运维人员信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DvOperationUser dvOperationUser)
    {
        dvOperationUser.setOperationUserCode(GenerateCodeUtils.getSequnce());
        dvOperationUser.setCreateBy(getUsername());
        dvOperationUser.setCreateTime(DateUtils.getNowDate());
        dvOperationUser.setUpdateBy(getUsername());
        dvOperationUser.setUpdateTime(DateUtils.getNowDate());
        dvOperationUser.setDeptId(getDeptId().toString());
        dvOperationUser.setCreateUserId(getUserId().toString());
        return toAjax(dvOperationUserService.insertDvOperationUser(dvOperationUser));
    }

    /**
     * 修改运维人员信息
     */
    @PreAuthorize("@ss.hasPermi('device:operation:edit')")
    @Log(title = "运维人员信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DvOperationUser dvOperationUser)
    {
        dvOperationUser.setUpdateBy(getUsername());
        dvOperationUser.setUpdateTime(DateUtils.getNowDate());
        return toAjax(dvOperationUserService.updateDvOperationUser(dvOperationUser));
    }

    /**
     * 删除运维人员信息
     */
    @PreAuthorize("@ss.hasPermi('device:operation:remove')")
    @Log(title = "运维人员信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        List<DvOperationUser> removeList = new ArrayList<>();
        List<DvDeployment> unbindManagerList = new ArrayList<>(); //需要接触绑定的投放设备
        for (Long id : ids) {
            DvOperationUser dvOperationUser = dvOperationUserService.selectDvOperationUserById(id); // 遍历每一个选择删除的人员id
            DvDeployment dvDeployment = new DvDeployment();
            dvDeployment.setOperationUserCode(dvOperationUser.getOperationUserCode()); //人员id作为 投放设备的 查询参数
            List<DvDeployment> dvDeployments = dvDeployDeviceManagerService.selectDvDeploymentList(dvDeployment); // 获取与该人员id相关的投放设备集合
            for (DvDeployment dvmanager : dvDeployments) { // 遍历所有的设备，并将要删除的人员名称和人员编码设置进去
                dvmanager.setDeleteOperationUserCode(dvOperationUser.getOperationUserCode());
                dvmanager.setDeleteOperationUserName(dvOperationUser.getOperationUserName());
                unbindManagerList.add(dvmanager);
            }
            dvOperationUser.setUpdateBy(getUsername());
            dvOperationUser.setUpdateTime(DateUtils.getNowDate());
            dvOperationUser.setStatus(1);
            removeList.add(dvOperationUser);
        }
        if (unbindManagerList.size() != 0) {
            return AjaxResult.error("运维人员已与投放设备绑定,需要将运维人员与指定投放设备进行解绑",unbindManagerList);
        }
        /*return toAjax(dvOperationUserService.tombstoneOperationUser(ids));*/
        int i = 0;
        for (DvOperationUser dvOperationUser : removeList) {
            i = dvOperationUserService.updateDvOperationUser(dvOperationUser);
        }
        return toAjax(i);
    }
}
